#!/bin/bash
# Origin: https://github.com/titpetric/netdata
# SPDX-License-Identifier: CC0 1.0 Universal
set -e
DEBIAN_FRONTEND=noninteractive

# some mirrors have issues, i skipped httpredir in favor of an eu mirror

#echo "deb http://ftp.nl.debian.org/debian/ stretch main" > /etc/apt/sources.list
#echo "deb http://security.debian.org/debian-security stretch/updates main" >> /etc/apt/sources.list
>/etc/apt/sources.list
echo 'deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib'>/etc/apt/sources.list
echo 'deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib'>>/etc/apt/sources.list
echo 'deb http://mirrors.aliyun.com/debian-security stretch/updates main'>>/etc/apt/sources.list
echo 'deb-src http://mirrors.aliyun.com/debian-security stretch/updates main'>>/etc/apt/sources.list
echo 'deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib'>>/etc/apt/sources.list
echo 'deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib'>>/etc/apt/sources.list

# install dependencies for build

apt-get update
apt-get -y install wget zlib1g-dev uuid-dev libmnl-dev gcc make curl git autoconf autogen automake pkg-config netcat-openbsd jq libuv1-dev liblz4-dev libjudy-dev libssl-dev
apt-get -y install autoconf-archive lm-sensors nodejs python python-mysqldb python-yaml libjudydebian1 libuv1 liblz4-1 openssl
apt-get -y install msmtp msmtp-mta apcupsd fping

# download netdata-v1.19.0.tar.gz
cd /
wget --tries=50 -c https://github.com/netdata/netdata/releases/download/v1.19.0/netdata-v1.19.0.tar.gz
tar zxvf netdata-v1.19.0.tar.gz
cd /netdata-v1.19.0

# use the provided installer

./netdata-installer.sh --dont-wait --dont-start-it

# remove build dependencies

rm -rf /netdata-v1.19.0.tar.gz
rm -rf /netdata-v1.19.0
dpkg -P zlib1g-dev uuid-dev libmnl-dev gcc make git autoconf autogen automake pkg-config libuv1-dev liblz4-dev libjudy-dev libssl-dev
apt-get -y autoremove
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*


# symlink access log and error log to stdout/stderr

ln -sf /dev/stdout /var/log/netdata/access.log
ln -sf /dev/stdout /var/log/netdata/debug.log
ln -sf /dev/stderr /var/log/netdata/error.log
